﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFTestClient.TFS;

namespace WCFTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a TFS Server name or full URL (http://{servername}:8080 ): ");
            String serverName = Console.ReadLine();
            Console.Write("Press '1' to run the BuildStatus Test or '2' for the Project Status Test: ");
            String testType = Console.ReadLine();

            switch (testType)
            {
                case "1":
                    BuildStatusTest(serverName);
                    break;
                case "2" :
                    ProjectStatusTest(
                        serverName, 
                        "Code Review Issue");
                    break;
                 default:
                    Console.WriteLine("Sorry, that option is not availabe!");
                    break;
            }

            Console.Write("\r\nPress Enter to close..");
            Console.ReadLine();
        }

        private static void BuildStatusTest(String serverName)
        {
            TFS.TfsServiceClient client = new WCFTestClient.TFS.TfsServiceClient();
            TeamProject[] teamProjects;

            try
            {
                teamProjects = client.GetBuildStatus(serverName);

                if (!teamProjects.Count().Equals(0))
                {
                    foreach (TeamProject project in teamProjects)
                    {
                        Console.WriteLine("Project name: {0}", project.Name);
                        foreach (BuildType build in project.BuildTypes)
                        {
                            Console.WriteLine("\tBuild : {0} is in '{1}' state.", build.Name, build.Status);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No projects found with active Build Types...");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\r\nAn error occured: {0}", ex.Message);
            }
        }

        private static void ProjectStatusTest(
            String serverName, 
            String workItemTypeName)
        {
            TFS.TfsServiceClient client = new WCFTestClient.TFS.TfsServiceClient();
            TeamProject[] teamProjects;

            try
            {
                teamProjects = client.GetProjectStatus(
                    serverName,
                    workItemTypeName);
            }
            catch (Exception ex)
            {

            }
        }
    }
}
